package com.traffic.between.util;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 公共工具类
 * 
 */
public class UtilsUtils {

	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(UtilsUtils.class);

	/**
	 * double精度调整
	 * 
	 * @param doubleValue
	 *            需要调整的值123.454
	 * @param format
	 *            目标样式".##"
	 * @return
	 */
	public static String decimalFormatToString(double doubleValue, String format) {
		DecimalFormat myFormatter = new DecimalFormat(format);
		String formatValue = myFormatter.format(doubleValue);
		return formatValue;
	}

	/**
	 * 获取UUID by jdk
	 * 
	 * @return
	 */
	public static String getUuidByJdk(boolean is32bit) {
		String uuid = UUID.randomUUID().toString();
		if (is32bit) {
			return uuid.toString().replace("-", "");
		}
		return uuid;
	}

	/**
	 * 获取订ID
	 * 
	 * @param lg
	 * @return
	 */
	public static String getOrderId(int lg) {
		String uuid = getUuidByJdk(true);
		return "F" + uuid.substring(0, lg);
	}

	/**
	 * 获取订ID
	 * 
	 * @param lg
	 * @return
	 */
	public static String getOrderId(int lg, String prefix) {
		String uuid = getUuidByJdk(true);
		return prefix + uuid.substring(0, lg);
	}

	/**
	 * 判断 字符串、Map、List是否为空
	 * 
	 * @param obj
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public static boolean isBlank(Object obj) {
		if (obj == null) {
			return true;
		} else if (obj instanceof String) {
			return StringUtils.isBlank(((String) obj));
		} else if (obj instanceof Map) {
			return ((Map) obj).isEmpty();
		}
		if (obj instanceof List) {
			return ((List) obj).isEmpty();
		} else {
			return false;
		}
	}

	public static String CMCC = "CMCC";
	public static String UNICOM = "UNICOM";
	public static String TELECOM = "TELECOM";
	public static String UNKNWON = "UNKNWON";

	/**
	 * 获取运营商类型
	 * 
	 * @param phoneNum
	 * @return
	 */
	public static String getNet(String phoneNum) {
		String head3 = "";
		String head4 = "";
		phoneNum = phoneNum.trim();

		if (phoneNum == null || phoneNum.length() < 11) {
			return UNKNWON;
		} else {
			if (phoneNum.startsWith("+")) {
				phoneNum = phoneNum.substring(1);
			}
			if (phoneNum.startsWith("86")) {
				phoneNum = phoneNum.substring(2);
			}
		}

		if (phoneNum.length() != 11) {
			return UNKNWON;
		}

		head3 = phoneNum.substring(0, 3);
		head4 = phoneNum.substring(0, 4);

		if (head3.equals("135") || head3.equals("136") || head3.equals("137") || head3.equals("138")
				|| head3.equals("139") || head3.equals("150") || head3.equals("151") || head3.equals("152")
				|| head3.equals("157") || head3.equals("158") || head3.equals("159") || head3.equals("182")
				|| head3.equals("183") || head3.equals("184") || head3.equals("187") || head3.equals("188")
				|| head3.equals("147")) {
			return CMCC;
		}
		if (head4.equals("1340") || head4.equals("1341") || head4.equals("1342") || head4.equals("1343")
				|| head4.equals("1344") || head4.equals("1345") || head4.equals("1346") || head4.equals("1347")
				|| head4.equals("1348")) {
			return CMCC;
		}
		if (head3.equals("130") || head3.equals("131") || head3.equals("132") || head3.equals("145")
				|| head3.equals("155") || head3.equals("156") || head3.equals("175") || head3.equals("176")
				|| head3.equals("185") || head3.equals("186")) {
			return UNICOM;
		}
		if (head3.equals("133") || head3.equals("153") || head3.equals("180") || head3.equals("181")
				|| head3.equals("189")) {
			return TELECOM;
		}
		if (head4.equals("1349")) {
			return TELECOM;
		}

		return UNKNWON;
	}
}
